package cn.fu.excle;

import java.io.*;
/**
 * �ļ��Լ��ļ��еĴ�����ɾ������
 * @author Foldcc
 *
 */
public class FileUtils {
		/**
		 * ����Ŀ¼
		 * @param destDirName
		 * @return
		 */
		public boolean createDir(String destDirName) {
			File dir = new File(destDirName);
			if (dir.exists()) {// �ж�Ŀ¼�Ƿ����
				return false;
			}
			if (!destDirName.endsWith(File.separator)) {// ��β�Ƿ���"/"����
				destDirName = destDirName + File.separator;
			}
			if (dir.mkdirs()) {// ����Ŀ��Ŀ¼
				return true;
			} else {
				return false;
			}
		}
		
		
		public boolean createFile(String filePath){
			File file = new File(filePath);
			if(!file.exists()){
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
					return false;
				}
			}
			return true;
		}
		
		
		/**
		 * �޸��ļ���
		 * @param file
		 * @param newName
		 * @return
		 */
		public boolean sFileName(File file , String newName){
			file.renameTo(new File(newName));   //����  
			return true;
		}
		
		/***
		 * д��ָ��string��ָ���ı���
		 * @param Msg
		 * @param Path
		 */
		public void WriteBase(String Msg , String fileName){
			try {
		        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName), true)));
		        out.write(Msg);
		        out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		/**
		 * ��ȡָ��·���ı�����String
		 * @param path
		 * @return
		 */
		public String ReadBase(String path) {  
	        StringBuffer buffer = null;
			try {
				InputStream is;
				is = new FileInputStream(path);
				 BufferedReader in = new BufferedReader(new InputStreamReader(is));  
				 buffer = new StringBuffer();
			        String line = "";  
			        while ((line = in.readLine()) != null){  
			            buffer.append(line);  
			        }
			     in.close();
			     return buffer.toString();  
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        return null;  
	  
	    } 
		
		/**
		 * �ж�ָ���ļ���ַ�Ƿ���� ���ڷ���true
		 * @param destDirName
		 * @return
		 */
		public boolean existsDir(String destDirName){
			File dir = new File(destDirName);
			if (dir.exists()) {// �ж�Ŀ¼�Ƿ����
				return true;
			}
			return false;
		}
		
		 /**
		  *  �ж��ļ��Ƿ����
		  * @param file
		  */
		public boolean judeFileExists(String fileName) {
			File file = new File(fileName);
			if (file.exists()){
	             return true;
	         }
	         return false;
	     }
		
		
		/**
		 *  ɾ�������ļ�
		 * @param filePath
		 * @return
		 */
		public boolean deleteFile(String filePath) {
			boolean flag = false;
			File file = new File(filePath);
			if (file.isFile() && file.exists()) {// ·��Ϊ�ļ��Ҳ�Ϊ�������ɾ��
				file.delete();// �ļ�ɾ��
				flag = true;
			}
			return flag;
		}
		
		
		/**
		 * ɾ��Ŀ¼���ļ��У��Լ�Ŀ¼�µ��ļ�
		 * @param dirPath
		 * @return
		 */
		public boolean deleteDirectory(String dirPath) {
			// ���sPath�����ļ��ָ�����β���Զ�����ļ��ָ���
			if (!dirPath.endsWith(File.separator)) {
				dirPath = dirPath + File.separator;
			}
			File dirFile = new File(dirPath);
			// ���dir��Ӧ���ļ������ڣ����߲���һ��Ŀ¼�����˳�
			if (!dirFile.exists() || !dirFile.isDirectory()) {
				return false;
			}
			boolean flag = true;
			File[] files = dirFile.listFiles();// ��ô���·���µ������ļ�
			for (int i = 0; i < files.length; i++) {// ѭ������ɾ���ļ����µ������ļ�(������Ŀ¼)
				if (files[i].isFile()) {// ɾ�����ļ�
					flag = deleteFile(files[i].getAbsolutePath());
					if (!flag)
						break;// ���ɾ��ʧ�ܣ�������
				} else {// ���õݹ飬ɾ����Ŀ¼
					flag = deleteDirectory(files[i].getAbsolutePath());
					if (!flag)
						break;// ���ɾ��ʧ�ܣ�������
				}
			}
			if (!flag)
				return false;
			if (dirFile.delete()) {// ɾ����ǰĿ¼
				return true;
			} else {
				return false;
			}
		}
}
